<?php
namespace catchAdmin\externalGoods\model;

use catcher\base\CatchModel;
use think\Exception;
use think\facade\Db;

class CreditDeduction extends CatchModel
{

//    protected $name = 'db_syst_sync_goods_category';
    protected $name = 'db_credit_deduction_transactions';
    protected $field = [
        'id',
        'order_pk',
        'order_no',
        'amount',
        'reason',
        'account_residue',
        'transaction_type',
        'is_increase',
        'is_refunded',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id'
    ];

    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        return $this
//            ->field('id,pid,level,name')
//            ->withoutField(['updated_at','created_at','deleted_at','tenant_id','creator_id','supplier_id'], true)
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate();
//            ->toArray();
    }

    public function checkUnique($where, $id = 0, $pk = 'id')
    {
        try {
            if (empty($id)) {
                $has = $this->field($pk)->where($where)->find();
            } else {
                $has = $this->field($pk)->where($where)->where($pk, '<>', $id)->find();
            }
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        return $has;
    }
}
